package com.cd.controller;

import com.cd.common.controller.BaseController;
import com.cd.common.domain.AjaxResult;
import com.cd.common.exception.LqqException;
import com.cd.common.util.AuthUtils;
import com.cd.schedule.jpa.domain.ScheduleEntity;
import com.cd.schedule.jpa.repo.ScheduleEntityRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.cd.schedule.api.CalenderService;

/**
 * 日程表相关功能
 *
 * @author lqq
 */
@RestController
@RequestMapping("/calender")
public class CalenderController extends BaseController {

    @Autowired
    CalenderService calenderService;

    private static final Logger logger = LoggerFactory.getLogger(CalenderController.class);

    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public AjaxResult detail(){//找最近一个月的日程

        return success(calenderService.findSchedulesByUserId(AuthUtils.currentUserId()));
    }

    @RequestMapping(value = "/detail/{userId}",method = RequestMethod.GET)
    public AjaxResult detail(@PathVariable( value = "userId", required = true) Long userId){//找对应用户最近一个月的日程
        //目前仅允许找管理员的行程
        if (userId!=1L){
            throw new LqqException("您暂未获得访问该用户的权限");
        }
        return success(calenderService.findSchedulesByUserId(userId));
    }

    /**
     * @Description 这里预期了三种场景。
     * A：前端后端分别删除
     * B：后端删除，同时返回最新数据
     * C：后端删除，前端使用get请求再拿一次
     * 这边我们采用A 用户体验较好
     * @Author lqq
     * @Date 2025/4/1 15:24
     **/
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    public AjaxResult remove(@RequestBody ScheduleEntity schedule){//只能删除自己的
        logger.info("remove schedule is {}",schedule);
        if (schedule.getUserId()!=AuthUtils.currentUserId()){
            logger.error("remove schedule is {},but user is {}",schedule,AuthUtils.currentUserId());
            return error("不允许操作其他人的日程");
        }
        return success(calenderService.removeSchedulesByUserId(schedule.getScheduleId()));
    }
    /**
     * @Description 更新选B方案 要保持一致性
     * 实际这里有个bug 不知道为什么schedule不会传递 导致更新变成新增出现脏数据
     * 有瓶颈的话后面优化 这个可读性强
     * @Author lqq
     * @Date 2025/4/1 17:17
     **/
    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public AjaxResult modify(@RequestBody ScheduleEntity schedule){//只能删除自己的
        logger.info("modify schedule is {}",schedule);
        if (schedule.getUserId()!=AuthUtils.currentUserId()){
            logger.error("modify schedule is {},but user is {}",schedule,AuthUtils.currentUserId());
            return error("不允许操作其他人的日程");
        }
        calenderService.modifySchedules(schedule);
        return success(calenderService.findSchedulesByUserId(AuthUtils.currentUserId()));
    }


    @RequestMapping(value = "/removeById",method = RequestMethod.POST)
    public AjaxResult removeById(@RequestBody ScheduleEntity schedule){//只能由管理员删除其他人的 或者其他有权限的
        if (AuthUtils.currentUserId()!=1L){
            return error("您暂未访问该用户的权限");
        }
        return success(calenderService.removeSchedulesByUserId(schedule.getUserId()));
    }
}
